package com.guimin.order.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.guimin.order.dto.AddRecycleDto;
import com.guimin.order.entity.GuiminFeijiuOrder;
import com.guimin.order.service.GuiminFeijiuOrderService;
import com.guimin.user.api.UserInfoApi;
import com.guimin.user.constant.UserConstant;
import com.guimin.user.dto.LoginInfoDto;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 废旧回收订单(GuiminFeijiuOrder)控制层
 *
 * @author makejava
 * @since 2021-11-25 15:28:19
 */
@RestController
@RequestMapping("guiminFeijiuOrder")
public class GuiminFeijiuOrderApi {
    /**
     * 服务对象
     */
    @Resource
    private GuiminFeijiuOrderService guiminFeijiuOrderService;
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redis;
    @Resource
    private UserInfoApi userInfoApi;

    /**
     * 获取废旧回收订单
     *
     * @param current 当前页
     * @param size    每页大小
     * @param request r
     * @return r
     */
    @GetMapping("list")
    public R<IPage<GuiminFeijiuOrder>> list(HttpServletRequest request,
                                            @RequestParam(defaultValue = "1") Integer current,
                                            @RequestParam(defaultValue = "10") Integer size) {
        LoginInfoDto login = userInfoApi.getLogin(request);
        IPage<GuiminFeijiuOrder> page = guiminFeijiuOrderService.getOrderListByUserId(login.getId(), current, size);
        return R.ok(page);
    }

    /**
     * 获取订单详情
     *
     * @param request
     * @param orderId 订单id
     * @return
     */
    @GetMapping("get/by/id")
    public R<GuiminFeijiuOrder> getOneById(HttpServletRequest request, @RequestParam Long orderId) {
        LoginInfoDto login = userInfoApi.getLogin(request);
        GuiminFeijiuOrder order = guiminFeijiuOrderService.getOne(new LambdaQueryWrapper<GuiminFeijiuOrder>()
                .eq(GuiminFeijiuOrder::getId, orderId)
                .nested(i -> i.eq(GuiminFeijiuOrder::getPayeeId, login.getId())
                        .or().eq(GuiminFeijiuOrder::getPayerId, login.getId())
                ));
        return R.ok(order);
    }

    /**
     * 添加废旧回收订单
     *
     * @param request h
     * @param dto     订单数据
     * @return r
     */
    @PostMapping("/add/order")
    public R<Boolean> addOrder(HttpServletRequest request, @RequestBody @Validated AddRecycleDto dto) {
        if (true) {
            return R.failed("功能建设中");
        }
        LoginInfoDto login = userInfoApi.getLogin(request);
        if (!UserConstant.Role.ADMIN.value.equals(login.getRole())) {
            return R.failed("权限不足");
        }
        dto.setPayerId(login.getId());
        boolean save = guiminFeijiuOrderService.addRecycle(login.getId(), dto);
        return save ? R.ok(null) : R.failed("添加失败");
    }
}